<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%
Object obj=request.getSession().getAttribute("role_resource");
if(null==obj){
	response.setStatus(1000);
	return;
}
%> 
<script type="text/javascript">
	var dep_add_update = 0;
	
	var store = new Ext.data.JsonStore({
		root:"data",
    	totalProperty: "total",
		fields: ["id","name","remark"],
		url: "depart!listByOrg.action"
	});
	
	var dep_panel = {
		id: "dep_panel",
		xtype: "grid",
		stripeRows:true,
		loadMask : true,//在加载数据时遮罩住元素,替换元素Updater的加载提示器，并且在初始化加载之后销毁
		autoExpandColumn:"remark",//自动填充
		autoExpandMin : 100,//自动填充最小宽度
		store: store,
		headersDisabled: true,
		columns:[
        	new Ext.grid.RowNumberer({header: "序号", width:35}),
        	{header: "部门编号", dataIndex: "id", menuDisabled: true},
        	{header: "部门名称", dataIndex: "name", menuDisabled: true},
			{header: "备注", dataIndex: "remark",id:"remark", menuDisabled: true}
		],
		bbar: new Ext.ux.PagingToolbar({
			id:"depart_paging",
        	pageSize:limit,
        	store: store
    	}),
    	buttonAlign:"center",
		buttons:[
		{text:"新增", handler: function(){
			var node=Ext.getCmp("orgTree").getSelectionModel().getSelectedNode();
			if(node==null){
				//Ext.Msg.alert("提示", "请先选择部门节点！");
				Ext.Msg.show({
					   title:"警告",
					   msg: "请先选择机构节点!",
					   buttons: Ext.Msg.OK,
					   animEl: "elId",
					   icon:Ext.MessageBox.WARNING
					});
				return;
			}
				var panel = this.ownerCt.ownerCt.ownerCt;
				var manager_dep = panel.getComponent("manager_dep");
				manager_dep.resetParam();
				panel.layout.setActiveItem(1);
			
		}},{text:"修改", handler: function(){
			var updateOrg = this.ownerCt.ownerCt;
			var panel = updateOrg.ownerCt;
			var record = updateOrg.getSelectionModel().getSelected();
			
			if (record){
				panel.getComponent("manager_dep").setParam(record.data);
				panel.layout.setActiveItem(1);
			}else{
				Ext.Msg.show({
					   title:"提示",
					   msg:"请选择要修改的部门！",
					   buttons:Ext.Msg.OK,
					   icon: Ext.MessageBox.WARNING
					});
				}
			}
		},{text:"删除", handler: function(){
			var delOrg = this.ownerCt.ownerCt;
			var record = delOrg.getSelectionModel().getSelected();
			if(!record){
				Ext.Msg.show({
					   title:"提示",
					   msg:"请选择要删除的部门！",
					   buttons:Ext.Msg.OK,
					   icon: Ext.MessageBox.WARNING
				});
				return;
			}
			Ext.Msg.show({
				   title:"提示",
				   msg:"是否确认删除该数据?",
				   buttons:Ext.Msg.OKCANCEL,
				   fn:function(bnt,text){
					   if(bnt=="ok"){
						   Ext.Ajax.request({
							   params: {'person.depNo':record.id},
							   url: 'person!listByDepart.action',
							   success: function(response){
								   var dl = eval("("+response.responseText+")");
								   if(dl.total>0){
									   Ext.Msg.show({
										   title:"提示",
										   msg:"部门下面有用户成员，不能删除此部门！",
										   buttons:Ext.Msg.OK,
										   icon: Ext.MessageBox.WARNING
										});
									   return;
								   }else{
									   Ext.Ajax.request({
											params: {id:record.id},
											url: "depart!delete.action",
											success:function(form,action){
												Ext.Msg.show({
													   title:"提示",
													   msg:"部门删除成功!",
													   buttons:Ext.Msg.OK,
													   icon: Ext.MessageBox.INFO
													});
												store.load({params:{start : 0,limit :limit}});
												Ext.getCmp("depart_paging").doRefresh();
											}
									   });
								   }
							   }
						   });
					   }
				   },
				   icon: Ext.MessageBox.WARNING
			});
			/**/
			
		}
		}]
	};

	var manager_dep = {
		id: "manager_dep",
	    labelWidth: 60,
		xtype: "form",
	    bodyStyle:"padding:5px 5px 0",
		defaultType: "textfield",
		items: [{
			itemId: "dep_no",
			fieldLabel: "部门编号",
			hidden:true,
			anchor:"100%",
			name: "id",
			vtype:"alphanum"
		},{
			id: "depname",
			itemId: "dep_name",
			fieldLabel: "部门名称",
			anchor:"100%",
			name: "name",
			vtype:"alphanumchinese",
			validator:function (val){
        		if(val=="")return "该输入项为必输项！";
        		if(Ext.fucCheckCHLength(val)>50){
        			return "请输入50个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	},
        	listeners: {
        		'blur': function(){
        			var depname = this.ownerCt.getComponent("dep_name").getValue();
        			if(depname.length>0){
        				Ext.Ajax.request({
        					params: {"depart.oid": Ext.getCmp("orgTree").getSelectionModel().getSelectedNode().id, 
        							"depname": depname},
							url: 'depart!checkDep.action',
							success: function(response){
								var d=eval("("+response.responseText+")");
								if(d.code == "1"){
									Ext.getCmp("depname").setValue("");
									Ext.Msg.show({
			   							title:"提示",
			   							msg:"该部门名称已存在，请重新输入！",
			   							buttons:Ext.Msg.OK,
			   							icon: Ext.MessageBox.ERROR
									});
								}
							}
        				});
        			}
        		}
        	}
		},{
			itemId: "dep_org",
			fieldLabel: "所属机构",
			disabled:true,
			anchor:"100%",
			name: "organization"
		},{
			itemId: "org_id",
			fieldLabel: "所属机构ID",
			anchor:"100%",
			hidden:true,
			name: "oid"
		},{
			itemId: "dep_remark",
			fieldLabel: "备注",
			anchor:"100%",
			name: "remark",
			vtype:"alphanumchinese",
			validator:function (val){
        		if(val=="")return true;
        		if(Ext.fucCheckCHLength(val)>200){
        			return "请输入200个字符范围内的值！<br>注：一个中文字符算两个字符的长度！";
        		}
        		return true;
        	}
			
		}],
		buttonAlign:"center",
		buttons:[
			{text:"保存",id:"save", handler: function(e){
				if(!Ext.getCmp("manager_dep").getForm().isValid()){
					//Ext.Msg.alert("提示","验证不通过！");
					Ext.Msg.show({
						   title:"提示",
						   msg:"验证不通过!",
						   buttons:Ext.Msg.OK,
						   icon: Ext.MessageBox.ERROR
						});
					return;
				}
				
				e.setDisabled(true);
				var p2 = this.ownerCt.ownerCt;
				var dep_no = p2.getComponent("dep_no").getValue(),
					dep_name = p2.getComponent("dep_name").getValue(),
					dep_org = p2.getComponent("dep_org").getValue(),
					dep_remark = p2.getComponent("dep_remark").getValue();
				var panel = p2.ownerCt;
				var dep_panel = panel.getComponent("dep_panel");
				if(dep_add_update == 0){
					//添加
					p2.getForm().submit({
						clientValidation: true,
						url: "depart!add.action",
						success:function(form,action){
							Ext.Msg.show({
								   title:"提示",
								   msg:"操作成功!",
								   buttons:Ext.Msg.OK,
								   icon: Ext.MessageBox.INFO
								});
							
							Ext.getCmp("save").setDisabled(false);
							Ext.getCmp("depart_paging").doRefresh();
							panel.layout.setActiveItem(0);
						}
					});
				}else{
					//修改
					p2.getForm().submit({
						clientValidation: true,
						url: "depart!update.action",
						success:function(form,action){
							//Ext.Msg.alert("SUCCESS", action.result.msg);
							Ext.Msg.show({
								   title:"提示",
								   msg:"操作成功!",
								   buttons:Ext.Msg.OK,
								   icon: Ext.MessageBox.INFO
								});
							
							e.setDisabled(false);
							Ext.getCmp("depart_paging").doRefresh();
							panel.layout.setActiveItem(0);
							dep_add_update = 0 ;
						}
					});
				}

			}},{text:"取消", handler: function(){
				this.ownerCt.ownerCt.ownerCt.layout.setActiveItem(0);
			}}
		],
		setParam: function(data){
			var node=Ext.getCmp("orgTree").getSelectionModel().getSelectedNode();
			var me = this;
			me.getComponent("dep_no").setValue(data.id);
			me.getComponent("dep_name").setValue(data.name);
			me.getComponent("dep_org").setValue(node.text);
			me.getComponent("org_id").setValue(node.id);
			me.getComponent("dep_remark").setValue(data.remark);
			dep_add_update = 1;
		},
		resetParam: function(){
			var node=Ext.getCmp("orgTree").getSelectionModel().getSelectedNode();
			var me = this;
			me.getComponent("dep_no").setValue("");
			me.getComponent("dep_name").setValue("");
			me.getComponent("dep_org").setValue(node.text);
			me.getComponent("org_id").setValue(node.id);
			me.getComponent("dep_remark").setValue("");
			dep_add_update=0;
		}
	};
	
	
	var depart_pel = new Ext.Panel({
		id:"master_panel",
		border : false,
		layout: "border",
		items: [{
				id:"orgTree",
				xtype: "billTreePanel",
				dataUrl: "tree!organization.action",
				root: new Ext.tree.AsyncTreeNode({
					id: "root",
					expanded : true
				}),
				listeners: {
					"click": function(node){
						if (!node.id == "0"){
							store.baseParams = store.baseParams || {};
							store.baseParams["oid"] = node.id;
							store.load({params:{start : 0,limit :limit}});
							Ext.getCmp("myPanel").layout.setActiveItem(0);
						}
					}
				}
			}, {
				id: "myPanel",
				region: "center",
				layout:"card",
				border : false, 
				activeItem:0,//显示第一个容器
				items: [dep_panel,manager_dep]
		}]
	});	

	var CenterPanel=Ext.getCmp("CenterPanel");
	CenterPanel.removeAll();
	CenterPanel.add(depart_pel);
	CenterPanel.doLayout();
</script>